<?php

if(!$flow->isNewRecord) {
    // Set up database connection.
	$connectionString = StringUtil::getConnectionString(Yii::app()->db->connectionString, 
		Yii::app()->db->driverName, 
		Yii::app()->db->username, 
		Yii::app()->db->password);
	$db = ezcDbFactory::create($connectionString);

    // Set up workflow definition storage (database).
    $definition = new ezcWorkflowDatabaseDefinitionStorage($db);

    // Load latest version of workflow named by id.
    $workflow = $definition->loadByName($flow->workflow_name);

    // Set up workflow definition storage (XML).
    $definition = new ezcWorkflowDefinitionStorageXml(Yii::app()->runtimePath . '/');

    $xmlPath = Yii::app()->runtimePath . "/{$flow['workflow_name']}_{$flow['workflow_version']}.xml";
    if(file_exists($xmlPath)) {
        unlink($xmlPath);
    }
    // Save workflow definition to database.
    $definition->save($workflow);
    $flow->xml = file_get_contents($xmlPath);
}
?>

<?php /** @var BootActiveForm $form */
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
    'id'=>'create-activity',
    'type'=>'horizontal',
    'enableClientValidation'=>true,
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
    ),
)); ?>

<fieldset>

    <legend>工作流 - <?php echo $flow->isNewRecord ? '新建': $flow['workflow_name']?></legend>
    <?php echo $form->textFieldRow($flow, 'workflow_name'); ?>
    <?php echo $form->textAreaRow($flow, 'xml', array('class'=>'span11', 'rows'=>30)); ?>
</fieldset>
<div class="form-actions">
    <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'primary', 'icon'=>'ok white', 'label'=>$flow->isNewRecord ? '创 建' : '保 存')); ?>
    <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'reset', 'icon'=>'remove', 'label'=>'取 消')); ?>
</div>
<?php $this->endWidget(); ?>

